package com.k52group.vn.micromarket;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.k52group.vn.micromarket.R;
import com.k52group.vn.micromarket.adapter.MessageAdapter;
import com.k52group.vn.micromarket.classes.Message;
import com.k52group.vn.micromarket.classes.Profile;
import com.k52group.vn.micromarket.utils.Constant;
import com.k52group.vn.micromarket.utils.HttpRequest;
import com.k52group.vn.micromarket.utils.Utils;

public class InboxActivity extends Activity {
	ListView lv;
	List<Message> messages = new ArrayList<Message>();
//	Button btprofile;
	Profile profile = new Profile();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.inbox);
		lv = (ListView) findViewById(R.id.listView1);
		lv.setVisibility(View.GONE);
//		btprofile = (Button) findViewById(R.id.Button01);
		String data = Utils.getData(Constant.USER, this);
		profile = new Gson().fromJson(data, Profile.class);
//		if (profile.getUserId() == 0)
//			btprofile.setVisibility(View.INVISIBLE);

		new LoadDataTask().execute();

	}

	public void onClick(View v) {
//		Intent it = new Intent(this, RegisterActivity.class);
//		String profile = Utils.getData(Constant.USER, this);
//		it.putExtra("data", profile);
//		startActivity(it);
	}

	private class LoadDataTask extends AsyncTask<String, String, String> {
		ProgressDialog dialog;
		boolean isFalse = false;

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			dialog = new ProgressDialog(InboxActivity.this);
			dialog.setMessage("Loading ...");
			dialog.show();
		}

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			try {
				HttpRequest request = HttpRequest.post(Constant.URL_MESSAGE);
				request.form("reciever", profile.getUserId());
				messages = new Gson().fromJson(request.body(),
						new TypeToken<ArrayList<Message>>() {
						}.getType());
				if (messages == null)
					messages = new ArrayList<Message>();

			} catch (Exception e) {
				isFalse = true;
			}
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			dialog.dismiss();
			if (isFalse)
				Toast.makeText(InboxActivity.this, "Can't get data !",
						Toast.LENGTH_SHORT).show();
			else {
				MessageAdapter adapter = new MessageAdapter(InboxActivity.this,
						messages, profile);
//				if (profile.getUserId() == 0)
//					adapter.setGuest(true);
				lv.setAdapter(adapter);
				if (messages.size() > 0)
					lv.setVisibility(View.VISIBLE);
			}
		}
	}
}
